home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / src / doc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-14  |  14.8 KB  |  557 lines

  1. /* Record indices of function doc strings stored in a file.
  2.    Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <config.h>
  22.  
  23. #include <sys/types.h>
  24. #include <sys/file.h>    /* Must be after sys/types.h for USG and BSD4_1*/
  25.  
  26. #ifdef USG5
  27. #include <fcntl.h>
  28. #endif
  29.  
  30. #ifdef HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif
  33.  
  34. #ifndef O_RDONLY
  35. #define O_RDONLY 0
  36. #endif
  37.  
  38. #include "lisp.h"
  39. #include "paths.h"
  40. #include "buffer.h"
  41. #include "keyboard.h"
  42.  
  43. Lisp_Object Vdoc_file_name;
  44.  
  45. extern Lisp_Object Voverriding_local_map;
  46.  
  47. Lisp_Object
  48. get_doc_string (filepos)
  49.      long filepos;
  50. {
  51.   char buf[512 * 32 + 1];
  52.   register int fd;
  53.   register char *name;
  54.   register char *p, *p1;
  55.   register int count;
  56.   extern char *index ();
  57.  
  58.   if (XTYPE (Vdoc_directory) != Lisp_String
  59.       || XTYPE (Vdoc_file_name) != Lisp_String)
  60.     return Qnil;
  61.  
  62.   name = (char *) alloca (XSTRING (Vdoc_directory)->size
  63.               + XSTRING (Vdoc_file_name)->size + 8);
  64.   strcpy (name, XSTRING (Vdoc_directory)->data);
  65.   strcat (name, XSTRING (Vdoc_file_name)->data);
  66. #ifdef VMS
  67. #ifndef VMS4_4
  68.   /* For VMS versions with limited file name syntax,
  69.      convert the name to something VMS will allow.  */
  70.   p = name;
  71.   while (*p)
  72.     {
  73.       if (*p == '-')
  74.     *p = '_';
  75.       p++;
  76.     }
  77. #endif /* not VMS4_4 */
  78. #ifdef VMS4_4
  79.   strcpy (name, sys_translate_unix (name));
  80. #endif /* VMS4_4 */
  81. #endif /* VMS */
  82.  
  83.   fd = open (name, O_RDONLY, 0);
  84.   if (fd < 0)
  85.     error ("Cannot open doc string file \"%s\"", name);
  86.   if (0 > lseek (fd, filepos, 0))
  87.     {
  88.       close (fd);
  89.       error ("Position %ld out of range in doc string file \"%s\"",
  90.          filepos, name);
  91.     }
  92.   p = buf;
  93.   while (p != buf + sizeof buf - 1)
  94.     {
  95.       count = read (fd, p, 512);
  96.       p[count] = 0;
  97.       if (!count)
  98.     break;
  99.       p1 = index (p, '\037');
  100.       if (p1)
  101.     {
  102.       *p1 = 0;
  103.       p = p1;
  104.       break;
  105.     }
  106.       p += count;
  107.     }
  108.   close (fd);
  109.   return make_string (buf, p - buf);
  110. }
  111.  
  112. DEFUN ("documentation", Fdocumentation, Sdocumentation, 1, 2, 0,
  113.   "Return the documentation string of FUNCTION.\n\
  114. Unless a non-nil second argument is given, the\n\
  115. string is passed through `substitute-command-keys'.")
  116.   (function, raw)
  117.      Lisp_Object function, raw;
  118. {
  119.   Lisp_Object fun;
  120.   Lisp_Object funcar;
  121.   Lisp_Object tem, doc;
  122.  
  123.   fun = Findirect_function (function);
  124.  
  125.   switch (XTYPE (fun))
  126.     {
  127.     case Lisp_Subr:
  128.       if (XSUBR (fun)->doc == 0) return Qnil;
  129.       if ((int) XSUBR (fun)->doc >= 0)
  130.     doc = build_string (XSUBR (fun)->doc);
  131.       else
  132.     doc = get_doc_string (- (int) XSUBR (fun)->doc);
  133.       break;
  134.       
  135.     case Lisp_Compiled:
  136.       if (XVECTOR (fun)->size <= COMPILED_DOC_STRING)
  137.     return Qnil;
  138.       tem = XVECTOR (fun)->contents[COMPILED_DOC_STRING];
  139.       if (XTYPE (tem) == Lisp_String)
  140.     doc = tem;
  141.       else if (XTYPE (tem) == Lisp_Int && XINT (tem) >= 0)
  142.     doc = get_doc_string (XFASTINT (tem));
  143.       else
  144.     return Qnil;
  145.       break;
  146.  
  147.     case Lisp_String:
  148.     case Lisp_Vector:
  149.       return build_string ("Keyboard macro.");
  150.  
  151.     case Lisp_Cons:
  152.       funcar = Fcar (fun);
  153.       if (XTYPE (funcar) != Lisp_Symbol)
  154.     return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
  155.       else if (EQ (funcar, Qkeymap))
  156.     return build_string ("Prefix command (definition is a keymap associating keystrokes with\n\
  157. subcommands.)");
  158.       else if (EQ (funcar, Qlambda)
  159.            || EQ (funcar, Qautoload))
  160.     {
  161.       tem = Fcar (Fcdr (Fcdr (fun)));
  162.       if (XTYPE (tem) == Lisp_String)
  163.         doc = tem;
  164.       else if (XTYPE (tem) == Lisp_Int && XINT (tem) >= 0)
  165.         doc = get_doc_string (XFASTINT (tem));
  166.       else
  167.         return Qnil;
  168.  
  169.       break;
  170.     }
  171.       else if (EQ (funcar, Qmocklisp))
  172.     return Qnil;
  173.       else if (EQ (funcar, Qmacro))
  174.     return Fdocumentation (Fcdr (fun), raw);
  175.  
  176.       /* Fall through to the default to report an error.  */
  177.  
  178.     default:
  179.       return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
  180.     }
  181.  
  182.   if (NILP (raw))
  183.     {
  184.       struct gcpro gcpro1;
  185.  
  186.       GCPRO1 (doc);
  187.       doc = Fsubstitute_command_keys (doc);
  188.       UNGCPRO;
  189.     }
  190.   return doc;
  191. }
  192.  
  193. DEFUN ("documentation-property", Fdocumentation_property, Sdocumentation_property, 2, 3, 0,
  194.   "Return the documentation string that is SYMBOL's PROP property.\n\
  195. This is like `get', but it can refer to strings stored in the\n\
  196. `etc/DOC' file; and if the value is a string, it is passed through\n\
  197. `substitute-command-keys'.  A non-nil third argument avoids this\n\
  198. translation.")
  199.   (sym, prop, raw)
  200.      Lisp_Object sym, prop, raw;
  201. {
  202.   register Lisp_Object tem;
  203.  
  204.   tem = Fget (sym, prop);
  205.   if (XTYPE (tem) == Lisp_Int)
  206.     tem = get_doc_string (XINT (tem) > 0 ? XINT (tem) : - XINT (tem));
  207.   if (NILP (raw) && XTYPE (tem) == Lisp_String)
  208.     return Fsubstitute_command_keys (tem);
  209.   return tem;
  210. }
  211.  
  212. /* Scanning the DOC files and placing docstring offsets into functions.  */
  213.  
  214. static void
  215. store_function_docstring (fun, offset)
  216.      Lisp_Object fun;
  217.      int offset;
  218. {
  219.   fun = indirect_function (fun);
  220.  
  221.   /* The type determines where the docstring is stored.  */
  222.  
  223.   /* Lisp_Subrs have a slot for it.  */
  224.   if (XTYPE (fun) == Lisp_Subr)
  225.     XSUBR (fun)->doc = (char *) - offset;
  226.  
  227.   /* If it's a lisp form, stick it in the form.  */
  228.   else if (CONSP (fun))
  229.     {
  230.       Lisp_Object tem;
  231.  
  232.       tem = XCONS (fun)->car;
  233.       if (EQ (tem, Qlambda) || EQ (tem, Qautoload))
  234.     {
  235.       tem = Fcdr (Fcdr (fun));
  236.       if (CONSP (tem) &&
  237.           XTYPE (XCONS (tem)->car) == Lisp_Int)
  238.         XFASTINT (XCONS (tem)->car) = offset;
  239.     }
  240.       else if (EQ (tem, Qmacro))
  241.     store_function_docstring (XCONS (fun)->cdr, offset);
  242.     }
  243.  
  244.   /* Bytecode objects sometimes have slots for it.  */
  245.   else if (XTYPE (fun) == Lisp_Compiled)
  246.     {
  247.       /* This bytecode object must have a slot for the
  248.      docstring, since we've found a docstring for it.  */
  249.       if (XVECTOR (fun)->size <= COMPILED_DOC_STRING)
  250.     abort ();
  251.  
  252.       XFASTINT (XVECTOR (fun)->contents[COMPILED_DOC_STRING]) = offset;
  253.     }
  254. }
  255.  
  256.  
  257. DEFUN ("Snarf-documentation", Fsnarf_documentation, Ssnarf_documentation,
  258.   1, 1, 0,
  259.   "Used during Emacs initialization, before dumping runnable Emacs,\n\
  260. to find pointers to doc strings stored in `etc/DOC...' and\n\
  261. record them in function definitions.\n\
  262. One arg, FILENAME, a string which does not include a directory.\n\
  263. The file is found in `../etc' now; found in the `data-directory'\n\
  264. when doc strings are referred to later in the dumped Emacs.")
  265.   (filename)
  266.      Lisp_Object filename;
  267. {
  268.   int fd;
  269.   char buf[1024 + 1];
  270.   register int filled;
  271.   register int pos;
  272.   register char *p, *end;
  273.   Lisp_Object sym, fun, tem;
  274.   char *name;
  275.   extern char *index ();
  276.  
  277. #ifndef CANNOT_DUMP
  278.   if (NILP (Vpurify_flag))
  279.     error ("Snarf-documentation can only be called in an undumped Emacs");
  280. #endif
  281.  
  282.   CHECK_STRING (filename, 0);
  283.  
  284. #ifndef CANNOT_DUMP
  285.   name = (char *) alloca (XSTRING (filename)->size + 14);
  286. #ifdef RELPATH_DOC
  287.   strcpy (name, RELPATH_DOC);
  288. #else
  289.   strcpy (name, "../etc/");
  290. #endif
  291. #else /* CANNOT_DUMP */
  292.   CHECK_STRING (Vdoc_directory, 0);
  293.   name = (char *) alloca (XSTRING (filename)->size +
  294.               XSTRING (Vdoc_directory)->size + 1);
  295.   strcpy (name, XSTRING (Vdoc_directory)->data);
  296. #endif /* CANNOT_DUMP */
  297.   strcat (name, XSTRING (filename)->data);     /*** Add this line ***/
  298. #ifdef VMS
  299. #ifndef VMS4_4
  300.   /* For VMS versions with limited file name syntax,
  301.      convert the name to something VMS will allow.  */
  302.   p = name;
  303.   while (*p)
  304.     {
  305.       if (*p == '-')
  306.     *p = '_';
  307.       p++;
  308.     }
  309. #endif /* not VMS4_4 */
  310. #ifdef VMS4_4
  311.   strcpy (name, sys_translate_unix (name));
  312. #endif /* VMS4_4 */
  313. #endif /* VMS */
  314.  
  315.   fd = open (name, O_RDONLY, 0);
  316.   if (fd < 0)
  317.     report_file_error ("Opening doc string file",
  318.                Fcons (build_string (name), Qnil));
  319.   Vdoc_file_name = filename;
  320.   filled = 0;
  321.   pos = 0;
  322.   while (1)
  323.     {
  324.       if (filled < 512)
  325.     filled += read (fd, &buf[filled], sizeof buf - 1 - filled);
  326.       if (!filled)
  327.     break;
  328.  
  329.       buf[filled] = 0;
  330.       p = buf;
  331.       end = buf + (filled < 512 ? filled : filled - 128);
  332.       while (p != end && *p != '\037') p++;
  333.       /* p points to ^_Ffunctionname\n or ^_Vvarname\n.  */
  334.       if (p != end)
  335.     {
  336.       end = index (p, '\n');
  337.       sym = oblookup (Vobarray, p + 2, end - p - 2);
  338.       if (XTYPE (sym) == Lisp_Symbol)
  339.         {
  340.           /* Attach a docstring to a variable?  */
  341.           if (p[1] == 'V')
  342.         {
  343.           /* Install file-position as variable-documentation property
  344.              and make it negative for a user-variable
  345.              (doc starts with a `*').  */
  346.           Fput (sym, Qvariable_documentation,
  347.             make_number ((pos + end + 1 - buf)
  348.                      * (end[1] == '*' ? -1 : 1)));
  349.         }
  350.  
  351.           /* Attach a docstring to a function?  */
  352.           else if (p[1] == 'F')
  353.         store_function_docstring (sym, pos + end + 1 - buf);
  354.  
  355.           else
  356.         error ("DOC file invalid at position %d", pos);
  357.         }
  358.     }
  359.       pos += end - buf;
  360.       filled -= end - buf;
  361.       bcopy (end, buf, filled);
  362.     }
  363.   close (fd);
  364.   return Qnil;
  365. }
  366.  
  367. DEFUN ("substitute-command-keys", Fsubstitute_command_keys,
  368.   Ssubstitute_command_keys, 1, 1, 0,
  369.   "Substitute key descriptions for command names in STRING.\n\
  370. Return a new string which is STRING with substrings of the form \\=\\[COMMAND]\n\
  371. replaced by either:  a keystroke sequence that will invoke COMMAND,\n\
  372. or \"M-x COMMAND\" if COMMAND is not on any keys.\n\
  373. Substrings of the form \\=\\{MAPVAR} are replaced by summaries\n\
  374. \(made by describe-bindings) of the value of MAPVAR, taken as a keymap.\n\
  375. Substrings of the form \\=\\<MAPVAR> specify to use the value of MAPVAR\n\
  376. as the keymap for future \\=\\[COMMAND] substrings.\n\
  377. \\=\\= quotes the following character and is discarded;\n\
  378. thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output.")
  379.   (str)
  380.      Lisp_Object str;
  381. {
  382.   unsigned char *buf;
  383.   int changed = 0;
  384.   register unsigned char *strp;
  385.   register unsigned char *bufp;
  386.   int idx;
  387.   int bsize;
  388.   unsigned char *new;
  389.   Lisp_Object tem;
  390.   Lisp_Object keymap;
  391.   unsigned char *start;
  392.   int length;
  393.   Lisp_Object name;
  394.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  395.  
  396.   if (NILP (str))
  397.     return Qnil;
  398.  
  399.   CHECK_STRING (str, 0);
  400.   tem = Qnil;
  401.   keymap = Qnil;
  402.   name = Qnil;
  403.   GCPRO4 (str, tem, keymap, name);
  404.  
  405.   /* KEYMAP is either nil (which means search all the active keymaps)
  406.      or a specified local map (which means search just that and the
  407.      global map).  If non-nil, it might come from Voverriding_local_map,
  408.      or from a \\<mapname> construct in STR itself..  */
  409.   keymap = Voverriding_local_map;
  410.  
  411.   bsize = XSTRING (str)->size;
  412.   bufp = buf = (unsigned char *) xmalloc (bsize);
  413.  
  414.   strp = (unsigned char *) XSTRING (str)->data;
  415.   while (strp < (unsigned char *) XSTRING (str)->data + XSTRING (str)->size)
  416.     {
  417.       if (strp[0] == '\\' && strp[1] == '=')
  418.     {
  419.       /* \= quotes the next character;
  420.          thus, to put in \[ without its special meaning, use \=\[.  */
  421.       changed = 1;
  422.       *bufp++ = strp[2];
  423.       strp += 3;
  424.     }
  425.       else if (strp[0] == '\\' && strp[1] == '[')
  426.     {
  427.       Lisp_Object firstkey;
  428.  
  429.       changed = 1;
  430.       strp += 2;        /* skip \[ */
  431.       start = strp;
  432.  
  433.       while ((strp - (unsigned char *) XSTRING (str)->data
  434.           < XSTRING (str)->size)
  435.          && *strp != ']')
  436.         strp++;
  437.       length = strp - start;
  438.       strp++;        /* skip ] */
  439.  
  440.       /* Save STRP in IDX.  */
  441.       idx = strp - (unsigned char *) XSTRING (str)->data;
  442.       tem = Fintern (make_string (start, length), Qnil);
  443.       tem = Fwhere_is_internal (tem, keymap, Qt, Qnil);
  444.  
  445.       /* Disregard menu bar bindings; it is positively annoying to
  446.          mention them when there's no menu bar, and it isn't terribly
  447.          useful even when there is a menu bar.  */
  448.       if (!NILP (tem))
  449.         {
  450.           firstkey = Faref (tem, make_number (0));
  451.           if (EQ (firstkey, Qmenu_bar))
  452.         tem = Qnil;
  453.         }
  454.  
  455.       if (NILP (tem))    /* but not on any keys */
  456.         {
  457.           new = (unsigned char *) xrealloc (buf, bsize += 4);
  458.           bufp += new - buf;
  459.           buf = new;
  460.           bcopy ("M-x ", bufp, 4);
  461.           bufp += 4;
  462.           goto subst;
  463.         }
  464.       else
  465.         {            /* function is on a key */
  466.           tem = Fkey_description (tem);
  467.           goto subst_string;
  468.         }
  469.     }
  470.       /* \{foo} is replaced with a summary of the keymap (symbol-value foo).
  471.      \<foo> just sets the keymap used for \[cmd].  */
  472.       else if (strp[0] == '\\' && (strp[1] == '{' || strp[1] == '<'))
  473.     {
  474.       struct buffer *oldbuf;
  475.  
  476.       changed = 1;
  477.       strp += 2;        /* skip \{ or \< */
  478.       start = strp;
  479.  
  480.       while ((strp - (unsigned char *) XSTRING (str)->data
  481.           < XSTRING (str)->size)
  482.          && *strp != '}' && *strp != '>')
  483.         strp++;
  484.       length = strp - start;
  485.       strp++;            /* skip } or > */
  486.  
  487.       /* Save STRP in IDX.  */
  488.       idx = strp - (unsigned char *) XSTRING (str)->data;
  489.  
  490.       /* Get the value of the keymap in TEM, or nil if undefined.
  491.          Do this while still in the user's current buffer
  492.          in case it is a local variable.  */
  493.       name = Fintern (make_string (start, length), Qnil);
  494.       tem = Fboundp (name);
  495.       if (! NILP (tem))
  496.         {
  497.           tem = Fsymbol_value (name);
  498.           if (! NILP (tem))
  499.         tem = get_keymap_1 (tem, 0, 1);
  500.         }
  501.  
  502.       /* Now switch to a temp buffer.  */
  503.       oldbuf = current_buffer;
  504.       set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
  505.  
  506.       if (NILP (tem))
  507.         {
  508.           name = Fsymbol_name (name);
  509.           insert_string ("\nUses keymap \"");
  510.           insert_from_string (name, 0, XSTRING (name)->size, 1);
  511.           insert_string ("\", which is not currently defined.\n");
  512.           if (start[-1] == '<') keymap = Qnil;
  513.         }
  514.       else if (start[-1] == '<')
  515.         keymap = tem;
  516.       else
  517.         describe_map_tree (tem, 1, Qnil, Qnil, 0, 1);
  518.       tem = Fbuffer_string ();
  519.       Ferase_buffer ();
  520.       set_buffer_internal (oldbuf);
  521.  
  522.     subst_string:
  523.       start = XSTRING (tem)->data;
  524.       length = XSTRING (tem)->size;
  525.     subst:
  526.       new = (unsigned char *) xrealloc (buf, bsize += length);
  527.       bufp += new - buf;
  528.       buf = new;
  529.       bcopy (start, bufp, length);
  530.       bufp += length;
  531.       /* Check STR again in case gc relocated it.  */
  532.       strp = (unsigned char *) XSTRING (str)->data + idx;
  533.     }
  534.       else            /* just copy other chars */
  535.     *bufp++ = *strp++;
  536.     }
  537.  
  538.   if (changed)            /* don't bother if nothing substituted */
  539.     tem = make_string (buf, bufp - buf);
  540.   else
  541.     tem = str;
  542.   xfree (buf);
  543.   RETURN_UNGCPRO (tem);
  544. }
  545.  
  546. syms_of_doc ()
  547. {
  548.   DEFVAR_LISP ("internal-doc-file-name", &Vdoc_file_name,
  549.     "Name of file containing documentation strings of built-in symbols.");
  550.   Vdoc_file_name = Qnil;
  551.  
  552.   defsubr (&Sdocumentation);
  553.   defsubr (&Sdocumentation_property);
  554.   defsubr (&Ssnarf_documentation);
  555.   defsubr (&Ssubstitute_command_keys);
  556. }
  557.